
export const initSubcrib = () => {
	let keyArr = Object.keys(applyFun)
	console.log(keyArr)
	keyArr.forEach(item => {
		uni.setStorageSync(item, '')
	})
}

export const messageSubcrib = (name) => {
	// #ifdef MP-WEIXIN
	let { id, repeat } = applyFun[name]
	applySub(id, name, repeat)
	// #endif
}
const tips = {
	"MFvzKyQG6rCPMK1Q9BWHQftWgc26MS1O3eSIb_NuB5g": "服务订单提醒功能已关闭，是否前往开启？",
	"IJj0X-a0WX46J0-n_VkyzZ0ZT9pwCGfhEygYKbLV7pw": "服务订单状态通知功能已关闭，是否前往开启？",
	"3cBDPDX1iJov69j5epPo02PuhS0dWZ-d0wGT42sjiEE": "接单成功通知功能已关闭，是否前往开启？",
	"3Lte5HThKznz3NPL10ygJjVKitWU5VeL8pbk6hSfPg8": "订单取消通知功能已关闭，是否前往开启？",
	"WZN937YAaEz3EPoehfQTOOZZmH97d-BnFFbBJ89Gb1E": "审核结果通知功能已关闭，是否前往开启？",
	"TAzEFaALsLE9N6_qk_HaBLM72C3d22zslpp1ndSg7QY": "未读消息提醒功能已关闭，是否前往开启？"
}
const applyFun = {
	// //服务订单提醒 - 派单跟接单提醒
	// subscrib_01: {
	// 	id: ["MFvzKyQG6rCPMK1Q9BWHQftWgc26MS1O3eSIb_NuB5g"],
	// 	repeat: true,
	// },
	// //服务订单状态通知，接单成功通知 ，订单取消通知
	// subscrib_02: {
	// 	id: ["IJj0X-a0WX46J0-n_VkyzZ0ZT9pwCGfhEygYKbLV7pw", '3cBDPDX1iJov69j5epPo02PuhS0dWZ-d0wGT42sjiEE', '3Lte5HThKznz3NPL10ygJjVKitWU5VeL8pbk6hSfPg8'],
	// 	repeat: true,
	// },
	// //审核结果通知，接单成功通知
	// subscrib_03: {
	// 	id: ["WZN937YAaEz3EPoehfQTOJx6kRacdWy1ealNNzt2164", '3cBDPDX1iJov69j5epPo02PuhS0dWZ-d0wGT42sjiEE'],
	// 	repeat: true,
	// }
	
	//服务订单提醒 , 审核结果通知，未读消息提醒
	subscrib_01: {
		id: ["MFvzKyQG6rCPMK1Q9BWHQftWgc26MS1O3eSIb_NuB5g","WZN937YAaEz3EPoehfQTOJx6kRacdWy1ealNNzt2164",'TAzEFaALsLE9N6_qk_HaBKfYvWrTWNVFeK2XIafPxB8'],
		repeat: true,
	},
	//服务订单状态通知，接单成功通知 ，订单取消通知
	subscrib_02: {
		id: ["IJj0X-a0WX46J0-n_VkyzZ0ZT9pwCGfhEygYKbLV7pw", '3cBDPDX1iJov69j5epPo02PuhS0dWZ-d0wGT42sjiEE', '3Lte5HThKznz3NPL10ygJjVKitWU5VeL8pbk6hSfPg8'],
		repeat: true,
	}
}
function applySub(id : any, name : string, repeat : boolean) {
	if(repeat){
		uni.setStorageSync(name, "")
	}
	//当关闭repeat并且已经成功订阅时不在执行
	if (uni.getStorageSync(name) == '') {
		uni.requestSubscribeMessage({
			tmplIds: id,
			fail(err) {
				console.log("err", err)
				// uni.showModal({
				// 	title: '提示',
				// 	content: "消息订阅功能已关闭，是否前往开启？",
				// 	cancelText: '暂不开启',
				// 	confirmText: '去开启',
				// 	confirmColor: '#4acccc',
				// 	success: (res2) => {
				// 		if (res2.confirm) {
				// 			uni.openSetting({
				// 				withSubscriptions: true
				// 			})
				// 		} else if (res2.cancel) {
				// 		}
				// 	}
				// })
			},
			complete() {

			},
			success(res) {
				console.log("res", res)
				uni.setStorageSync(name, name)
				let currentId = ""
				for (let i = 0; i < id.length; i++) {
					console.log(res[id[i]])
					console.log(res[id[i]] != "accept" && res[id[i]] != "acceptWithAlert")
					if (res[id[i]] && res[id[i]] != "accept" && res[id[i]] != "acceptWithAlert") {
						currentId = id[i]
						return
					}
				}
				console.log(currentId)
				if(currentId){
					uni.showModal({
						title: '提示',
						content: tips[currentId],
						cancelText: '暂不开启',
						confirmText: '去开启',
						confirmColor: '#4acccc',
						success: (res2) => {
							if (res2.confirm) {
								uni.openSetting({
									withSubscriptions: true
								})
							} else if (res2.cancel) {
					
							}
						}
					})
				}else{
					// if(repeat){
					// 	uni.showToast({
					// 		icon:'none',
					// 		title:'已订阅成功！'
					// 	})
					// }
				}
				
			}
		})
	}
}